-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib: add defaultTo #357681
lib: add defaultTo #357681
Conversation
As a Haskeller, this function confused me briefly, as its A problem with the name "either" is that in natural language use, it does not convey a bias towards the one or the other. (And also the Haskell type suffers from this, coincidentally) These two reasons lead me to believe that a better name could be found for this function.
Definitiely a 👍 for adding a function that performs this pretty fundamental operation |
|
I feel like these don't read all that well in regular function form, but would read great with the pipe operator:
vs.
Alas, we are not there yet... But maybe looking ahead? As it stands, I feel like Edit: Actually my assumption with the pipe operator might be wrong, need to re-check the RFC. |
Of the names suggested so far,
I like Don't want to bikeshed, though - just another idea that shouldn't prevent this PR from being merged. |
I prefer |
After sleeping on it, my favorite so far is definitely lib.fallback foo.bar.baz "default" I read it as: fallback |
I agree this is a useful thing to have. first_non_null = builtins.foldl' either null; It'd operate on a list: nix-repl> first_non_null [ null null 5 null "test" null 7]
5
nix-repl> first_non_null [ true null null 5 null "test" null 7]
true
nix-repl> If we have |
If we're talking about a list It's worth considering a name that would be applicable to both variants in that case, though that certainly makes thing more difficult. Maybe I don't know... Personally I feel like the vast majority of the time I just want the basic |
Does it make sense to reverse the arguments? map (lib.fallback "default") [ "a" null "b" null ] This could be made clearer in the name, too, like |
I kinda prefer the fallback happening from left to right, but you're right that usually functions in Nix are written with partial application in mind, in which case it makes sense for the fallback value to come first. I'm happy to change it to any of these if there's consensus. |
3c1a433
to
23d8372
Compare
Changed the implementation to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Successfully created backport PR for |
I'm surprised this is not a thing yet. I swear I have a need for this function every single day.
If you have some long nullable expression like
foo.bar.baz
, or a function callfoo 123
, then usingif a != null then a else b
can get unnecessarily long, and even unnecessarily re-evaluated. So you usually need to wrap it into alet
binding likelet a = foo.bar.baz; if a != null then a else b
. Of course this is annoying.I propose adding a
lib.trivial.defaultTo
function to nixpkgs to handle this very common use case.Example usage:
Things done
nix.conf
? (See Nix manual)sandbox = relaxed
sandbox = true
nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD"
. Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/
)Add a 👍 reaction to pull requests you find important.